import keyboard
import threading
import ctypes
import getHandles
import collectSoul
import time
import destoryThings
import checkErrorState
import pyautogui
import my_keys
import mail
import utility
stopFlag = True

def useUselessThings():
    (point,fileName) = utility.findOneFromDir("./images/uselessThings/useableThings/",0.9)
    if point != None:
        for i in range(1,3):
            pyautogui.click(button="right",x=point.x,y=point.y)
            time.sleep(0.2)

def checkPet():
    petFlag = pyautogui.locateOnScreen("./images/pet.png",confidence = 0.9)
    food = pyautogui.locateCenterOnScreen("./images/food.png",confidence = 0.9)
    if petFlag != None and food != None  :
        for i in range(1,6):
            pyautogui.click(button="right",x=food.x,y=food.y)
            time.sleep(0.2)
def dropThings():
    (point,fileName) = utility.findOneFromDir("./images/dropThings/",0.95)
    if point != None:
        pyautogui.moveTo(point.x,point.y,0.5)
        pyautogui.click()
        things = pyautogui.locateCenterOnScreen("./images/things.png",confidence = 0.9)
        pyautogui.moveTo(things.x,things.y-50,0.5)
        time.sleep(0.2)
        pyautogui.click()
        time.sleep(0.5)
        dropDialog = pyautogui.locateCenterOnScreen("./images/dropDialog.png",confidence = 0.9)
        quantityDialog = pyautogui.locateCenterOnScreen("./images/quantityDialog.png",confidence = 0.9)
        if dropDialog != None and quantityDialog == None:
            ok = pyautogui.locateCenterOnScreen("./images/destory_ok.png",confidence = 0.9)
            pyautogui.moveTo(ok.x,ok.y,0.5)
            pyautogui.click()
            time.sleep(0.5)
            quantityDialog2 = pyautogui.locateCenterOnScreen("./images/quantityDialog.png",confidence = 0.9)
            if quantityDialog2 != None:
                cancel2 = pyautogui.locateCenterOnScreen("./images/cancelButton.png",confidence = 0.9)
                if cancel2 != None:
                    pyautogui.moveTo(cancel2.x,cancel2.y,0.5)
                    pyautogui.click()
        if dropDialog == None and quantityDialog != None:
            cancel = pyautogui.locateCenterOnScreen("./images/cancelButton.png",confidence = 0.9)
            if cancel != None:
                pyautogui.moveTo(cancel.x,cancel.y,0.5)
                pyautogui.click()
     
def func1():
    def run():
        global stopFlag
        stopFlag = True
        handleListIndex = 0
        print("autogame开始")
        try:
            while stopFlag:
                # 将当前窗口放到前台
                currentHandle = gameHandles[handleListIndex]
                ctypes.windll.user32.SetForegroundWindow(currentHandle)
                time.sleep(2)
                #关闭系统提示框
                sysRejectButton = pyautogui.locateCenterOnScreen("./images/sysDialogReject.png")
                if sysRejectButton != None:
                    pyautogui.click(sysRejectButton.x,sysRejectButton.y)
                #检测异常状态
                curState = checkErrorState.checkStates()
                if curState == 1:
                    handleListIndex = (handleListIndex + 1) % len(gameHandles)
                    time.sleep(8)
                    continue
                #集魂
                collectSoul.placeSoulStone()
                #检测宠物状态
                checkPet()
                pyautogui.moveTo(100,5)
                #使用垃圾药
                useUselessThings()
                pyautogui.moveTo(100,5)
                #捡东西
                #pickThings.pickUp(0x44)
                #销毁物品
                #destoryThings.destoryThing()
                #扔东西
                dropThings()
                pyautogui.moveTo(100,5)
                handleListIndex = (handleListIndex + 1) % len(gameHandles)
                time.sleep(6)
            print("autogame结束")
        except Exception as result:
            mail.sendMsg("程序异常退出")
            print(result)
            raise result
        
    t1 = threading.Thread(target=run)
    t1.start()

def func2():
    def run():
        global stopFlag
        stopFlag = False
    t2 = threading.Thread(target=run)
    t2.start()

def func3():
    def run():
        global stopFlag
        stopFlag = True
        while stopFlag:
            pyautogui.click()
    t3 = threading.Thread(target=run)
    t3.start()

#找出所有游戏窗口句柄

skipList = []
gameHandles= getHandles.getGameWindowHandles()
temp = None
while True:
    temp = input("请输入要忽略的窗口句柄,输入exit时结束")
    if temp == "exit":
        break
    skipList.append(int(temp,16))
print("skipList",skipList)
usefulList = []

for gameHandlesItem in gameHandles:
    flag = 0 
    for skipListItem in skipList:
        if gameHandlesItem == skipListItem:
            flag = 1
    if flag == 0:
        usefulList.append(gameHandlesItem)

print("usefulList",usefulList)    
gameHandles = usefulList
#print(gameHandles)

#开始自动脚本
keyboard.add_hotkey('ctrl + 1', func1)

keyboard.add_hotkey('ctrl + 2', func3)

#停止自动脚本
keyboard.add_hotkey('ctrl + 3', func2)


keyboard.wait('f12') 

